package core_handle

import (
	"fmt"
	"github.com/Unknwon/goconfig"
	"github.com/kataras/iris/v12"
	"iris_test/Tools"
	"iris_test/config"
	models "iris_test/models/supsite"
	"gopkg.in/mgo.v2/bson"
	"strings"
)

// 域名获取的中间健
func DomainHandle(ctx iris.Context) {
	host := ctx.Host()
	var listDomain = Tools.StrArray{"gt-api.westarcloud.com", "st-api.westarcloud.com", "plugin.westarcloud.com"}
	if listDomain.InArray(host) {
		Tools.NotFound(ctx)
		return
	}
	var obj = new(models.SiteDomain)
	defer obj.Close()
	res := obj.Init().GetSiteByDomain(host)
	if len(res) == 0 {
		Tools.NotFound(ctx)
	}
	if res["pcdomain"] == "" && strings.Contains(host, ".5xy.me") {
		res["pcdomain"] = fmt.Sprintf("%s.5xy.me", res["hostname"])
	} else if res["pcdomain"] == "" {
		subDomain, _ := config.Conf["config_url"].(*goconfig.ConfigFile).GetValue("supsite", "front")
		res["pcdomain"] = fmt.Sprintf("%s.%s", res["hostname"], subDomain)
	}
	config.Conf["host"] = host
	config.Conf["siteInfo"] = res
	config.Conf["siteid"] = res["_id"].(bson.ObjectId).Hex()
	ctx.Next()
}
